package com.daykw.wenshu.task;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.entity.Record;
import org.nutz.dao.impl.NutDao;

import com.daykw.wenshu.config.NutzDaoConfig;
import com.daykw.wenshu.model.SearchDailyUrl;
import com.daykw.wenshu.model.SearchSubDailyUrl;
import com.daykw.wenshu.utils.MD5Util;
import com.daykw.wenshu.utils.SpringContextHolder;

public class DailyDiffSubJob extends Thread {

	private static Logger logger = Logger.getLogger(DailyDiffSubJob.class);

	public void run() {
		try {
			NutDao dao = SpringContextHolder.getBean(NutzDaoConfig.class);
			List<SearchDailyUrl> clist = dao.query(SearchDailyUrl.class,
					Cnd.where("total", ">", 220).and("sub_status", "=", 0).limit(1,10));

			for (SearchDailyUrl daily : clist) {
				this.init(daily);
				dao.update(SearchDailyUrl.class, Chain.make("sub_status", 2), Cnd.where("id", "=", daily.getId()));
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void init(SearchDailyUrl daily) throws ParseException, UnsupportedEncodingException {

		NutDao dao = SpringContextHolder.getBean(NutzDaoConfig.class);

		List<Record> clist = dao.query("t_wenshu_case_type", null);

		for (Record r : clist) {
			String type = r.getString("type");

			String code = URLEncoder.encode(type, "utf-8");
			String url = daily.getUrl() + "&" + "conditions=searchWord+" + code + "+++%E5%85%B3%E9%94%AE%E8%AF%8D:"
					+ code;
			this.saveUrl(url, daily.getStart_date(), daily.getCity(), daily.getCourt_level(), type, dao);
		}

	}

	private void saveUrl(String url, String s, String c, String level, String type, NutDao dao) {
		SearchSubDailyUrl search = new SearchSubDailyUrl();
		search.setCreate_date(new Date());
		search.setPage(1);
		search.setUrl(url);
		search.setStatus(0);
		search.setStart_date(s);
		search.setEnd_date(s);
		search.setCity(c);
		search.setCourt_level(level);
		String md5 = MD5Util.getMD5(url);
		search.setMd5(md5);
		search.setCase_type(type);
		
		try {
			dao.fastInsert(search);
			logger.info("完成" + s + ",城市:" + c + ",type:" + type + "url成功");
		} catch (Exception e) {
			logger.error("", e);
		}
	}

	public Date localDateTime2Date(LocalDateTime localDateTime) {
		ZoneId zoneId = ZoneId.systemDefault();
		ZonedDateTime zdt = localDateTime.atZone(zoneId);
		Date date = Date.from(zdt.toInstant());
		return date;
	}

	public long getDaySub(String beginDateStr, String endDateStr) {
		long day = 0;
		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
		java.util.Date beginDate;
		java.util.Date endDate;
		try {
			beginDate = format.parse(beginDateStr);
			endDate = format.parse(endDateStr);
			day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
			// System.out.println("相隔的天数="+day);
		} catch (ParseException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return day;
	}

}
